﻿using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Packaging;
using OpenXml.Excel.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace OpenXml.Excel.Data
{
    //zms 2013/3/19
    /// <summary>
    /// Excel中单元格信息
    /// </summary>
    internal class CellData : IComparable<CellData>, IEqualityComparer<CellData>
    {
        //行号
        public int RowId { get; set; }

        //列号
        public int ColumnId { get; set; }

        //内容
        public string Value { get; set; }  //图片则存图片的名字

        //宽
        public int Width { get; set; }

        //高
        public int Height { get; set; }

        //数据类型
        public DataType DataType { get; set; }

        //编号
        public string Id { get; set; }


        public static int GetCoumnId(string num)
        {
            int lastindex = num.Length - 2;

            //最后一个字母如果是数字,则剔除
            if (Regex.IsMatch(num.Substring(num.Length - 1), "0-9"))
            {
                lastindex = num.Length - 3;
            }

            int columnId = 0, count = 0;

            /* A =65 返回1, AA 返回 27*/
            while (lastindex >= 0)
            {
                columnId = Convert.ToInt32((num[lastindex] - 64) * Math.Pow(26, count) + columnId);
                count++;
                lastindex--;
            }

            return columnId;
        }

        /// <summary>
        /// 实现 ICompareAlble接口方法
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(CellData other)
        {
            if (other == null)
                throw new Exception();

            if (this.RowId < other.RowId)
            {
                return -1;
            }
            else if (this.RowId > other.RowId)
            {
                return 1;
            }
            else
            {
                if (this.ColumnId < other.ColumnId)
                {
                    return -1;
                }
                else if (this.ColumnId > other.ColumnId)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }

            }
        }

        public static CellData DefaultCompare = new CellData();

        public bool Equals(CellData x, CellData y)
        {
            return x.RowId == y.RowId && x.ColumnId == y.ColumnId;
        }

        public int GetHashCode(CellData obj)
        {
            return 0;
        }


        /// <summary>
        /// 获取唯一键值
        /// </summary>
        /// <returns></returns>
        public string GetKey()
        {
            return GetKey(this.RowId, this.ColumnId);
        }

        /// <summary>
        /// 获取唯一键值
        /// </summary>
        /// <param name="rowId">行号</param>
        /// <param name="columnId">列号</param>
        /// <returns></returns>
        public static string GetKey(int rowId, int columnId)
        {
            return $"{rowId}_{columnId}";
        }

    }
}
